/*******************************************************************************
 * Copyright (c) 2011 Olexandr V. Shaposhnikov.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Olexandr V. Shaposhnikov - initial API and implementation and/or initial documentation
 *******************************************************************************/
package com.funkyroach.use.core.parser.antlr;

import org.antlr.runtime.RecognitionException;
import org.antlr.runtime.Token;
import org.antlr.runtime.TokenStream;

import com.funkyroach.use.core.parser.IUSParserProblems;
import com.funkyroach.use.core.parser.USToken;
import com.funkyroach.use.core.parser.antlr.USProblemReporter.Severity;
import com.funkyroach.use.plugin.USPlugin;

public class USParserImpl extends UnrealScriptParser {

    private final USProblemReporter reporter;

    public USParserImpl(TokenStream input, USProblemReporter reporter) {
        super(input);
        this.reporter = reporter;
    }

    public void reportError(RecognitionException re) {
        // if (re instanceof NoViableAltException) {
        // return;
        //
        // }
        final int start = re.token != null ? reporter.getOffset(re.token) + reporter.length(re.token) : 0;
        int end = reporter.getOffset(re.line, re.charPositionInLine);
        if (end < start) {
            end = start + 1;
        }

//        String[] tkNames = getTokenNames();
        String tn = re.token.getText();
//        if (tkNames.length >= re.c) {
//            tn = tokenNames[re.c];
//        } 
        reporter.setMessage(IUSParserProblems.SYNTAX_ERROR, "Syntax error: " + tn + " is unexpected");
        reporter.setSeverity(Severity.ERROR);
        reporter.setRange(start, end);
        reporter.setLine(re.line);
        reporter.report();
        USPlugin.log(re);
    }

    public void reportThrowable(Throwable th) {
        USPlugin.log(th);
        // reporter.reportProblem(new USProblem(th));
    }

    protected int startOf(Token t1) {
        if (t1 == null)
            return 0;
        return reporter.getOffset(t1);
    }

    protected int endOf(Token t1) {
        if (t1 == null)
            return 0;
        String text = t1.getText();
        int textStart = reporter.getOffset(t1);
        int textEnd = textStart + text.length();
        return textEnd;
    }

    protected void handlePPInclude(String path) {
        // TODO
        System.out.println("XXXXX" + path);
    }

    protected void handlePPIf(USToken tk, USToken tkId) {
        // TODO
        System.out.println("XXXXX" + tk.getText() + " ===== " + tkId.getText());
    }
    
    


}